<?php
class Boards extends Public_Controller
{
	public function __construct()
	{
		parent::__construct();
	}
	
	public function index($slug = 'main',$group_id = 1)
	{
		$data['color'][1] = 'green';
		$data['color'][2] = 'red';
		$data['color'][3] = 'orange';
		$data['color'][4] = 'blue';
		$data['color'][5] = 'green';
		$data['slug'] = $slug;
		$data['group'] = new Group($group_id);
		$category = new Category;
		$category->get_by_slug($slug);
		$where = false;
		
		$data['contents'] = $category->child->get_by_related_structure('type', 'board')->board;
		if(!empty($_GET['name']))$data['contents']->where('(firstname like \'%'.$_GET['name'].'%\' or lastname like \'%'.$_GET['name'].'%\' )');
		
		$data['contents']->where('group_id',$group_id);
		
		if($group_id==1)
		{
			$year = date('Y')+543;
			$data['contents']->where('((position = \'ผู้อำนวยการสำนักตรวจและประเมินผล\' and year_end >= '.$year.' and year_start <= '.$year.') or position <> \'ผู้อำนวยการสำนักตรวจและประเมินผล\') and group_id = 1');
			$data['contents']->order_by('row asc,col asc');
			$data['contents']->get();
		}
		elseif($group_id==5)
		{
			if(!empty($_GET['year']))
			{
				$year = (!empty($_GET['year']))?$_GET['year']:date('Y')+543;
				$data['contents']->where('year_end >= '.$year.' and year_start <= '.$year);
			}
			$data['contents']->order_by('col asc');
			$data['contents']->get();
			//$data['contents']->check_last_query();
		}
		else
		{
			$data['contents']->order_by('area asc,firstname asc');
			$data['contents']->get_page(20);
		}
		//$data['contents']->check_last_query();
		if($group_id<>1)
		{
			$data['boards'] = $category->child->get_by_related_structure('type', 'board')->board;
			$data['boards']->where('group_id',2);
			//$data['boards']->where('year_end >= '.$year.' and year_start <= '.$year);
			$data['boards']->order_by('row asc,col asc');
			$data['boards']->get();
		}
		
		if($group_id<>5)
		{
			$this->template->build('index', $data);
		}
		else
		{
			$this->template->build('hall',$data);
		}
		
	}
	
	
}
